#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<pair<int,int>,null_type,less<pair<int,int> >,
            rb_tree_tag,tree_order_statistics_node_update> order_set;
struct BST
{
    order_set s;
    void clear()
    {
        s.clear();
    }
    void insert(int k)
    {
        auto itr=s.upper_bound(make_pair(k-1,0));
        if(itr==s.end() || (*itr).first>k)
            s.insert(make_pair(k,-1));
        else
            s.insert(make_pair(k,(*itr).second-1));
    }
    void erase(int k)
    {
        auto itr=s.upper_bound(make_pair(k-1,0));
        if(itr!=s.end() && (*itr).first==k)
            s.erase(make_pair(k,(*itr).second));
    }
    int size()
    {
        return (int)s.size();
    }
    int count(int k)// <=k
    {
        return s.order_of_key(make_pair(k,0));
    }
    vector<int> get(int k)// <=k
    {
        vector<int> res;
        for(auto itr=s.begin();itr!=s.end();itr++)
        {
            if(itr->first >k)break;
            res.push_back(itr->first);
        }
        return res;
    }
}bst;
typedef long long ll;
const int MAXN=100005;
int A[MAXN],B[MAXN],pA[MAXN],pB[MAXN];
int x[MAXN],y[MAXN],r[MAXN],s[MAXN];
inline ll floor(ll x,ll y)
{
    if(y<0)x=-x,y=-y;
    if(x>=0)return x/y;
    else return (x-y+1)/y;
}
inline ll ceil(ll x,ll y)
{
    if(y<0)x=-x,y=-y;
    if(x>=0)return (x+y-1)/y;
    else return x/y;
}
ll sum(int p[],int n,ll m)
{
    ll cnt=0;
    bst.clear();
    bst.insert(0);
    for(int i=1;i<=n;i++)
    {
        cnt+=bst.count(p[i]-m);
        bst.insert(p[i]);
    }
    return cnt;
}
pair<ll,ll> cal(int p[],int n)
{
    ll l=-1e18,r=1e18;
    while(l<r)
    {
        ll m=l+(r-l)/2;
        if(sum(p,n,m)>=100000)l=m+1;
        else r=m;
    }
    ll t=l;
    l=-1e18,r=1e18;
    while(l<r)
    {
        ll m=l+(r-l+1)/2;
        if(sum(p,n,m)>=100000)l=m;
        else r=m-1;
    }
    return make_pair(t,l);
}
vector<int> figure_out(int p[],int n,int zr)
{
    ll k=min(sum(p,n,zr),100000LL);
    pair<ll,ll> bd=cal(p,n);
    bd.first=max(bd.first,1LL*zr);
    bd.second=max(bd.second,1LL*zr);
    vector<int> res;
    bst.clear();
    bst.insert(0);
    for(int i=1;i<=n;i++)
    {
        vector<int> tmp=bst.get(p[i]-bd.first);
        for(int j=0;j<(int)tmp.size();j++)
            res.push_back(p[i]-tmp[j]);
        bst.insert(p[i]);
    }
    while((int)res.size()<k)
        res.push_back(bd.second);
    if(zr)for(int i=0;i<(int)res.size();i++)
        res[i]=-res[i];
    return res;
}
int main()
{
    freopen("B-large.in","r",stdin);
    freopen("B.out","w",stdout);
    int T;
    scanf("%d",&T);
    for(int ca=1;ca<=T;ca++)
    {
        int n,k;
        scanf("%d%d",&n,&k);
        fprintf(stderr,"%d %d\n",ca,n);
        int C,D,E1,E2,F;
        scanf("%d%d%d%d%d%d%d",&x[1],&y[1],&C,&D,&E1,&E2,&F);
        for(int i=2;i<=n;i++)
        {
            x[i]=(C*x[i-1]+D*y[i-1]+E1)%F;
            y[i]=(D*x[i-1]+C*y[i-1]+E2)%F;
            r[i]=(C*r[i-1]+D*s[i-1]+E1)%2;
            s[i]=(D*r[i-1]+C*s[i-1]+E2)%2;
        }
        for(int i=1;i<=n;i++)
        {
            A[i]=(r[i] ? -1 : 1)*x[i];
            B[i]=(s[i] ? -1 : 1)*y[i];
        }
        for(int i=1;i<=n;i++)
        {
            pA[i]=pA[i-1]+A[i];
            pB[i]=pB[i-1]+B[i];
        }
        vector<int> keyA=figure_out(pA,n,0);
        vector<int> keyB=figure_out(pB,n,0);
        for(int i=1;i<=n;i++)
            pA[i]=-pA[i],pB[i]=-pB[i];
        vector<int> tmpA=figure_out(pA,n,1);
        vector<int> tmpB=figure_out(pB,n,1);
        for(int i=0;i<(int)tmpA.size();i++)
            keyA.push_back(tmpA[i]);
        for(int i=0;i<(int)tmpB.size();i++)
            keyB.push_back(tmpB[i]);
        sort(keyA.begin(),keyA.end());
        sort(keyB.begin(),keyB.end());
        ll l=-1e18,r=1e18;
        while(l<r)
        {
            ll m=l+(r-l+1)/2,cnt=0;
            for(int i=0;i<(int)keyA.size();i++)
            {
                if(keyA[i]>0)
                    cnt+=keyB.end()-lower_bound(keyB.begin(),keyB.end(),ceil(m,keyA[i]));
                else if(keyA[i]==0)
                    cnt+=(int)keyB.size()*(m<=0);
                else
                    cnt+=upper_bound(keyB.begin(),keyB.end(),floor(m,keyA[i]))-keyB.begin();
            }
            if(cnt>=k)l=m;
            else r=m-1;
        }
//        for(int i=0;i<(int)keyA.size();i++)
//            printf("%d ",keyA[i]);
//        printf("\n");
//        for(int i=0;i<(int)keyB.size();i++)
//            printf("%d ",keyB[i]);
//        printf("\n");
        printf("Case #%d: %lld\n",ca,l);
    }
    return 0;
}
